package com.yg.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.config.result.ResultCode;
import com.yg.entity.readAndWriteTemplates.TeacherEducationPromotionTemplate;
import com.yg.entity.singleEntity.Teacher;
import com.yg.entity.singleEntity.TeacherEducation;
import com.yg.entity.singleEntity.TeacherEducationPromotion;
import com.yg.service.TeacherEducationPromotionService;
import com.yg.service.TeacherEducationService;
import com.yg.service.TeacherService;
import com.yg.utils.DateValidationUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 对Sheet02进行操作
 */
public class TeacherEducationPromotionReadListener extends AnalysisEventListener<TeacherEducationPromotionTemplate> {


    private TeacherEducationPromotionService teacherEducationPromotionService;

    private TeacherService teacherService;

    private TeacherEducationService teacherEducationService;

    private R r;

    public TeacherEducationPromotionReadListener(TeacherEducationPromotionService teacherEducationPromotionService, TeacherService teacherService, TeacherEducationService teacherEducationService, R r) {
        this.teacherEducationPromotionService = teacherEducationPromotionService;
        this.teacherService = teacherService;
        this.teacherEducationService = teacherEducationService;
        this.r = r;
    }

    //表格信息
    private List<TeacherEducationPromotionTemplate> list = new ArrayList<>();

    //错误信息
    private List<String> errorList = new ArrayList<>();

    @Override
    public void invoke(TeacherEducationPromotionTemplate teacherEducationPromotionTemplate, AnalysisContext analysisContext) {
        System.out.println(teacherEducationPromotionTemplate);
        list.add(teacherEducationPromotionTemplate);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //学历提升集合
        List<TeacherEducationPromotion> teacherEducationPromotionList = new ArrayList<>();

        //教师表集合，进行学历提升之后要更改教师最高学历
        List<TeacherEducation> teacherEducationList = new ArrayList<>();

        for (TeacherEducationPromotionTemplate teacherEducationPromotionTemplate : list) {

            /*
                数据校验
             */
            String teacherId = teacherEducationPromotionTemplate.getTeacherId();
            String teacherName = teacherEducationPromotionTemplate.getName();

            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

            wrapper.eq("teacher_id", teacherId);
            wrapper.eq("name", teacherName);

            Teacher one = null;
            try {
                one = teacherService.getOne(wrapper);
                System.out.println("one：" + one);
            } catch (Exception exception) {
                exception.printStackTrace();
                System.out.println("此教师信息错误：" + teacherEducationPromotionTemplate.toString());
                errorList.add("此教师信息错误：" + teacherEducationPromotionTemplate.toString());
            }

            if (one != null) {

                //获得教师的学历信息
                TeacherEducation teacherEducation = null;
                try {
                    QueryWrapper<TeacherEducation> teacherEducationQueryWrapper = new QueryWrapper<>();
                    wrapper.eq("id", one.getId());
                    teacherEducation = teacherEducationService.getOne(teacherEducationQueryWrapper);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorList.add("此教师的学历信息错误：" + one.getTeacherId());
                }

                //将模板中的数据转换到学历提升实体类中
                TeacherEducationPromotion teacherEducationPromotion = new TeacherEducationPromotion();
                BeanUtils.copyProperties(teacherEducationPromotionTemplate, teacherEducationPromotion);

                //学历提升ID设置为Teacher的ID，并且从学历表中获得原专业技术职务并设置
                teacherEducationPromotion.setTeacherId(one.getTeacherId());
                //设置原专业技术职务
                teacherEducationPromotion.setOriginalProfessionalTechnical(teacherEducation.getProfessional());
                teacherEducationPromotionList.add(teacherEducationPromotion);

                //学历表中，Teacher的最高学历提升，改变最高学历，最高学位，最高学位获得时间，最高学历获得单位，最后专业研究方向
                teacherEducation.setEducation(teacherEducationPromotion.getQualifications());
                teacherEducation.setDegree(teacherEducationPromotion.getDegree());
                teacherEducation.setDegreeTime(DateValidationUtil.dateValidation(teacherEducationPromotion.getEndTime(),"yyyy.MM"));
                teacherEducation.setDegreeUnit(teacherEducationPromotion.getSchool());
                teacherEducation.setProfessional(teacherEducationPromotion.getProfessional());
                teacherEducationList.add(teacherEducation);
            } else {
                System.out.println("没有查询到此教师：" + teacherEducationPromotionTemplate.toString());
                errorList.add("没有查询到此教师：" + teacherEducationPromotionTemplate.toString());
            }
        }


        //假如没有报错，则将数据存入到数据库中
        if (errorList.isEmpty()) {
            //存入到数据库中
            teacherEducationPromotionService.saveBatch(teacherEducationPromotionList);
            teacherEducationService.updateBatchById(teacherEducationList);
        } else {
            r.code(ResultCode.ERROR);
            r.success(false);
            r.message(errorList.toString());
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {

        System.out.println("错误：" + exception.getMessage());
        errorList.add("错误：" + exception.getMessage());

    }
}
